<?php
if (!defined('FosfoaSystem'))
{
    exit(1);
}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class url extends general
{
    public function __construct()
    {
        parent::__construct();
        $this->tpl_title = "网址库";
    }
    function index()
    {
        $this->tt = "网址库管理";
        $this->ttype = "my";
        $u = $_SESSION["userinfo"];
        $jhuid = $u['uid'];
        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 10);

        $obj = spClass("urlModel");
        $conditions = array('1' => 1);
        $arr = $obj->spPager($current_page, $page_size)->findAll($conditions, 'id desc');
        $arrBar = $obj->spPager()->getPager();
        $this->tPageBar = $arrBar;
        $this->arr = $arr;
        $this->display("admin/url_index.html");
    }
    function add()
    {
        $this->action = 'add';
        $this->addtitle = "添加网址";
        $this->display("admin/url_add.html");
    }
    function edit()
    {
        $id = $this->spArgs("id");
        $obj = spClass("urlModel");
        $this->web = $obj->urlDetail($id);
        $this->action = 'edit';
        $this->addtitle = "编辑网址库";
        $this->display("admin/url_add.html");
    }
    function sy()
    {
        $this->jhsy = array(0 => '未审', 1 => '已审', );
        $jhid = $this->spArgs("jhid");
        $obj = spClass("jhModel");
        $this->jh = $obj->jhDetail($jhid);
        $this->addtitle = "审阅计划";
        $this->display("admin/jh_sy.html");

    }
    function del()
    {
        $Obj = spClass("urlModel");
        $id2 = $this->spArgs("id", 0);
        if ($id2 == 0)
        {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);

            foreach ($idsarray as $id)
            {
                $conditions = array('id' => $id);
                $Obj->delete($conditions); // 删除记录
            }
        } else
        {
            $conditions = array('id' => $id2);
            $Obj->delete($conditions); // 删除记录
        }
        $this->success(T('Successfully  deleted!'), spUrl("url", "index"));

    }
    function save()
    {
        //echo "正在载入....";
        $obj = spClass("urlModel");
        $cdx = spClass('Cdx');
        $edit = $this->spArgs("edit");
        $sy = $this->spArgs("sy");
        $sx = $this->spArgs("sx");
        $selids = $this->spArgs("selids");
        $selidsarray = explode(',', $selids);
        if ($edit <> "")
        {
            if ($sx == 1)
            { //判断是否是选择性刷新
                //echo $selids;
                //print_r($selidsarray);
                $conditions = "id in(" . $selids . ")"; //将选择的ID放进条件中查询
                $arr = $obj->findAll($conditions, "id desc", "id,url"); //根据条件获取到所选ID的数据数组
                //print_r($arr);exit;
                $ii = 0;
                foreach ($arr as $val)
                { //开始数组遍历单条网址的抓取和判断，存在，就更新数据，不存在，就插入新的数据
                    //print_r($val);
                    //echo $val['url'];
                    $request = $this->checkurl2(true, $val['url']); //单条网址的抓取数据并返回到$request中
                    //print_r($request);
                    //检查是否存在，并操作数据。
                    $url = $request['url'];
                    $title = urldecode($cdx->reTozc($request['title']));
                    $dec = urldecode($cdx->reTozc($request['description']));
                    $demo = $request['status'];

                    $demoarr = explode('|', $demo);
                    $urlray = explode('|', $url);
                    $title = explode('|', $title);
                    $dec = explode('|', $dec);
                    $iii = 0;
                    foreach ($urlray as $val)
                    {
                        $check = $obj->checkurl($val);
                        if (!$check['check'])
                        {
                            $arr2[] = $val;
                            $arr3[] = $demoarr[$iii];
                            $dec2[] = $dec[$iii];
                            $title[] = $title[$iii];

                        } else
                        {

                            //这里是更新已存在的。
                            $conditions = array('id' => $check['id']);
                            $data2 = array('title' => $title[$iii], 'des' => $dec[$iii], 'demo' => $demoarr[$iii],
                                'modtime' => time(), );
                            $obj->update($conditions, $data2); // 修改计划表公开或私有
                            //$this->success("审阅成功！", spUrl("jh","all"));
                        }
                        $iii = $iii + 1;
                    }

                    $i = 0;
                    foreach ($arr2 as $url)
                    {

                        $data2 = array('url' => $url, 'title' => $title[$i], 'des' => $dec[$i], 'demo' =>
                            $arr3[$i], );

                        $data2['addtime'] = time();
                        //$data['modtime'] =time();//date('y-m-d H:i:s',time())

                        $obj->create($data2); // 新增记录
                        $i = $i + 1;
                    }
                    $ii = $ii + 1;
                }


            }
            // echo $sy;
            // echo $jhsydemo;
            //$obj->update($conditions,$data2); // 修改计划表公开或私有
            $msg = "新增(" . $i . ")个----更新成功";
            $this->success($msg, spUrl("url", "index"));
        } else
        {
            $strAction = $this->spArgs("action");
            $id = $this->spArgs("id");
            $url = $this->spArgs("url");
            $title = $cdx->reTozc($this->spArgs("title"));
            $dec = $cdx->reTozc($this->spArgs("dec"));
            $demo = $this->spArgs("demo");

            $demoarr = explode('|', $demo);
            $urlray = explode('|', $url);
            $title = explode('|', $title);
            $dec = explode('|', $dec);
            $ii = 0;
            foreach ($urlray as $val)
            {
                $check = $obj->checkurl($val);
                if (!$check['check'])
                {
                    $arr2[] = $val;
                    $arr3[] = $demoarr[$ii];
                    $dec2[] = $dec[$ii];
                    $title2[] = $title[$ii];
                } else
                {

                    //这里是更新已存在的。
                    $data2 = array('title' => $title[$ii], 'des' => $dec[$ii], 'demo' => $demoarr[$ii],
                        'modtime' => time(), );
                    $conditions = array('id' => $id);
                    $obj->update($conditions, $data2);
                }
            }
            $ii = $ii + 1;
        }
        $i = 0;
        foreach ($arr2 as $url)
        {
            $data2 = array('url' => $url, 'title' => $title2[$i], 'des' => $dec2[$i], 'demo' =>
                $arr3[$i], 'addtime' => time(), );
            $obj->create($data2); // 修改计划表公开或私有
            $i = $i + 1;
        }
        $msg = $i . "个新网址添加成功";
        $result = array('status' => 1, // 标志
            'message' => $msg, // 提示信息
            );
        echo json_encode($result); // 返回（显示）JSON结果
    }
    function checkurl()
    {
        $a = $_POST["param"];
        $obj = spClass("urlModel");
        $check = $obj->checkurl($a);
        if ($check['check'])
        {
            //$this->success("不存在");
            //echo"<script>alert('用户名不能为空!')</script>";return false;";
            echo "失败，网址已存在.";
        } else
        {
            echo "y";
        }

    }
    function checkurl2($pl = false, $url3)
    {
        if ($pl)
        {
            $url = $url3;
        } else
        {
            $url = $this->spArgs("url");
        }
        $cdx = spClass('Cdx');
        $arr = $cdx->iscdx($url);

        //print_r($arr);//exit;


        //echo json_encode( $result );
        //echo count($arr)."<br/>";

        $i = 0;
        $arr_n = count($arr) - 1;


        //$title=urlencode(iconv('gb2312', 'utf-8',$arr[$arr_n]['title']));
        //$description=urlencode(iconv('gb2312', 'utf-8',$arr[$arr_n]['description']));
        //echo $title;echo $description;exit;
        foreach ($arr as $f)
        {
            $url2 .= $f['url'];
            $status .= $f['status'];
            $title .= urlencode(iconv('gb2312', 'utf-8', $f['title']));
            $description .= urlencode(iconv('gb2312', 'utf-8', $f['description']));
            //echo $arr_n."-";
            //echo $i."<br/>";
            if ($arr_n != $i)
            {
                $url2 .= "|";
                $status .= "|";
                $title .= "|";
                $description .= "|";
            }
            $i++;
        }
        //echo $u;
        $result = array('type' => 1, 'status' => $status, // 状态
            'url' => $url2, // 网址
            'title' => $title, //站名
            'description' => $description, //描述
            );
        if ($pl)
        {
            return $result;
        } else
        {
            echo json_encode($result); // 返回（显示）JSON结果
        }
        //print_r($result);
    }
}